import tools from "../../../tools"
import hierParentModel from './hierParentModel'

const idName = 'nodeId'

export default class routingModel extends hierParentModel {
  constructor(hierProperties, web) {
    super(hierProperties, routingModel)
    this.web = web || {}
    this.rootId = ''
  }
  set(item = {}) {
    this.hierProperties = item.hierProperties || []
    this.web = item.web || {}
    this.addRoot()
  }
  // 保存前, 格式化content数据
  formatContent() {
  }
  addRoot() {
    if (!this.hierProperties.content) {
      this.hierProperties = routingModel.tpl()
    }
    let rootData = this.find('nodeType', 'root')
    if (!rootData) {
      let rootId = '_root'
      let firstWCId = tools.uuid()
      let newRoot = routingModel.rootItemTpl({id: rootId})
      let newItem = routingModel.itemTpl({id: firstWCId, prev: [rootId]})

      this.web = {
        data: [
          this.webRootTpl({id: rootId}),
          {
            ...this.webWCTpl({
              id: firstWCId,
              parentId: rootId,
            }),
            fixed: false,
            width: 160,
            height: 80,
            x: 100,
            y: 160,
          },
          this.webLineTpl({
            to: firstWCId
          }),
        ]
      }
      this.rootId = rootId
      this.hierProperties.content = [
        newRoot,
        newItem,
      ]
    } else {
      this.rootId = rootData[idName]
    }
  }
  itemTpl(item={}) {
    return routingModel.itemTpl(item)
  }
  static tpl() {
    return {
      type: 'chain',
      content: [
        // item
      ]
    }
  }
  static rootItemTpl({id}) {
    id = id || tools.uuid();
    return {
      [idName]: id,
      prev: [], //前驱节点
      nodeType: "root",
      name: "root",
      properties: [{type:'String'}],
      statusProperties: [],
      link: {},
    }
  }
  static itemTpl(item={}) {
    let {id, prev} = item
    id = id || tools.uuid();
    return {
      [idName]: id,
      prev: prev || [], //前驱节点
      nodeType: "list",
      name: "",
      properties: [{type:'String'}],
      statusProperties: [],
      link: {},
    }
  }
  //#region web相关
  webRootTpl({id}) {
    id = id || tools.uuid()
    return {
        id,
        name: "root",
        type: "routingRoot",
        shapeType: "routingRoot",
        properties: {
            // id,
            // name: 'root',
            // link: {}
        },
        statusProperties: [],
        fixed: false,
        width: 160,
        height: 80,
        x: 100,
        y: 0,
    }
  }
  webWCTpl(item={}){
    let id = item.id || tools.uuid();
    return {
        parentId: item.parentId,
        id,
        type: "workCenter",
        shapeType: "workCenter",
        prev: [],
        properties: {
            // id,
            // name: 'work center',
            // link: item.link || {}
        },
        statusProperties: [],
    }
  }
  webLineTpl(item={}){
    return {
        id: tools.uuid(),
        type: "line",
        connectType: "elbow",
        cornersRadius: 0,
        customGap: 0,
        forwardArrow: "filled",
        from: item.from || "_root",
        to: item.to || "_0",
        fromSide: "bottom",
        toSide: "top",
        points: [
            {
                x: 180,
                y: 80
            },
            {
                x: 180,
                y: 160
            }
        ],
        width: 0,
        height: 60,
        stroke: "#2196F3",
        strokeWidth: 2,
        x: 100,
        y: 80
    }
  }
  //#endregion
}